﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace XStudio.XCRF.Data.Business.Archieve
{
    public static class ArchieveHelper
    {
        /// <summary>
        /// 创建实体存档
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static IArchieve CreateArchieve(this IArchieve entity)
        {
            Type entityType = entity.GetType();
            IArchieve archieve = (IArchieve)Activator.CreateInstance(entityType);
            PropertyInfo[] propertyInfoes = entityType.GetProperties();
            foreach (PropertyInfo propertyInfo in propertyInfoes)
            {
                object[] objs = propertyInfo.GetCustomAttributes(typeof(ArchieveFieldAttribute), false);
                if (objs.Length == 1)
                {
                    object value = propertyInfo.GetValue(entity, null);
                    propertyInfo.SetValue(archieve, value, null);
                }
            }
            archieve.MasterID = entity.ID;
            archieve.IsDeleted = true;
            return archieve;
        }
    }
}
